package com.wgj.android.utils;

import android.content.Context;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/8.
 */

public class AssetsBankInfo {
    private static String openBinNum(Context context) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String str = null;
        try {
            InputStream is = context.getResources().getAssets().open("binNum.txt");
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = is.read(bytes)) != -1) {
                outputStream.write(bytes, 0, length);
            }
            is.close();
            outputStream.close();
            str = outputStream.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    //获得Bank card的前缀
    private static List<Long> getBinNum(Context context) {
        String binNum = openBinNum(context);
        String[] binArr = binNum.split(",");
        List<Long> lon = new ArrayList<>();
        for (int i = 0; i < binArr.length; i++) {
            if (i % 2 == 0)
                lon.add(Long.parseLong(binArr[i]));

        }
        return lon;
    }

    //获得BankName

    private static List<String> getBinName(Context context) {
        String binNum = openBinNum(context);
        String[] binArr = binNum.split(",");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < binArr.length; i++) {
            if (i % 2 != 0)
                list.add(binArr[i]);
        }
        return list;
    }
    //通过输入的卡号获得银行卡信息
    public static String getNameOfBank(Context context, long binNum) {
        Log.e("sangfei.code", "bankBin: " + binNum);
        int index = 0;
        index = binarySearch(getBinNum(context), binNum);
        if (index == -1) {
            return "磁条卡卡号";
        }
        return getBinName(context).get(index);
    }

    //数量有上千条，利用二分查找算法来进行快速查找法

    public static int binarySearch(List<Long>  srcArray, long des){

        int low = 0;
        int high = srcArray.size()-1;
        while(low <= high)        {
            int middle = (low + high)/2;
            if(des == srcArray.get(middle)) {
                return middle;
            }  else if(des < srcArray.get(middle))  {
                high = middle - 1;
            }   else   {
                low = middle + 1;
            }
        }
        return -1;
    }
    public static String getID(String key){
        Map<String,String> map = new HashMap<String,String>();
        map.put("工商银行","1002");
        map.put("农业银行","1005");
        map.put("中国银行","1026");
        map.put("建设银行","1003");
        map.put("招商银行","1001");
        map.put("邮储银行","1066");
        map.put("交通银行","1020");
        //map.put("浦发银行","1004");
        map.put("民生银行","1006");
        map.put("兴业银行","1009");
        map.put("平安银行","1010");
        map.put("中信银行","1021");
        //map.put("华夏银行","1025");
        //map.put("广发银行","1027");
        //map.put("光大银行","1022");
        //map.put("北京银行","1032");
        //map.put("宁波银行","1056");
        String ID = map.get(key);
        return ID;
    }
    public static Map<String,String> getMap(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("工商银行","1002");
        map.put("农业银行","1005");
        map.put("中国银行","1026");
        map.put("建设银行","1003");
        map.put("招商银行","1001");
        map.put("邮政银行","1066");
        map.put("交通银行","1020");
        map.put("浦发银行","1004");
        map.put("民生银行","1006");
        map.put("兴业银行","1009");
        map.put("平安银行","1010");
        map.put("中信银行","1021");
        map.put("华夏银行","1025");
        map.put("广发银行","1027");
        map.put("光大银行","1022");
        map.put("北京银行","1032");
        map.put("宁波银行","1056");

        return map;
    }
}
